package com.gxuwz.dao.impl;

import com.gxuwz.dao.CourseDao;
import com.gxuwz.entity.Classes;
import com.gxuwz.entity.Course;
import com.gxuwz.entity.Department;
import com.gxuwz.entity.vo.ClassesVo;
import com.gxuwz.entity.vo.CourseVo;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CourseDaoImpl implements CourseDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public PageVo selectCourseAll(Course entity, PageInfo pageInfo,String instID) {
        String sql = "select courseID,className,courseName,year,term,hour from sys_course,sys_classes " +
                "where sys_course.del_flag = 0 and sys_course.classID = sys_classes.classID " +
                "and sys_classes.instID = ? and sys_classes.del_flag = 0 ";
        BeanPropertyRowMapper<CourseVo> courseBeanPropertyRowMapper = new BeanPropertyRowMapper<CourseVo>(CourseVo.class);
        List<Object> example = new ArrayList<Object>();
        PageUtil pageUtil = new PageUtil();
        if(entity==null){
            Object[] params = new Object[]{instID};
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, courseBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        } else {
            Object[] params;
            example.add(instID);
            if(entity.getCourseID() != null && !entity.getCourseID().equals("")){
                sql += "and courseID like ?";
                example.add("%"+entity.getCourseID()+"%");
            }
            if(entity.getCourseName() != null && !entity.getCourseName().equals("")){
                sql += "and courseName like ?";
                example.add("%"+entity.getCourseName()+"%");
            }
            if(entity.getYear() != null && !entity.getYear().equals("")){
                sql += "and year like ?";
                example.add("%"+entity.getYear()+"%");
            }
            if(entity.getTerm() != null && !entity.getTerm().equals("")) {
                sql += "and term like ?";
                example.add("%"+entity.getTerm()+"%");
            }
            params = example.toArray();
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, courseBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        }
    }

    public CourseVo selectCourseByID(String courseId) {
        String sql = "select courseID,className,courseName,year,term,hour,sys_classes.classID " +
                "from sys_course,sys_classes " +
                "where sys_course.classID = sys_classes.classID and courseID = ? " +
                "and sys_course.del_flag = 0 and sys_classes.del_flag = 0 ";
        BeanPropertyRowMapper<CourseVo> courseBeanPropertyRowMapper = new BeanPropertyRowMapper<CourseVo>(CourseVo.class);
        Object[] params = new Object[]{courseId};
        try{
            CourseVo courseVo = jdbcTemplate.queryForObject(sql,courseBeanPropertyRowMapper,params);
            return courseVo;
        }catch (Exception ex){
            System.out.println(ex);
            return null;
        }
    }

    public int insertCourse(Course entity) {
        String sql = "insert into sys_course values (?,?,?,?,?,?,0)";
        Object[] params = new Object[]{entity.getCourseID(),entity.getClassID(),entity.getCourseName(),entity.getYear(),entity.getTerm(),entity.getHour()};
        int insert = jdbcTemplate.update(sql,params);
        return insert;
    }

    public int updateCourse(Course entity, String oldId) {
        String sql = "update sys_course set courseID = ? , classID = ? , courseName = ? , year = ? , term = ? , hour = ? " +
                "where courseID = ?";
        Object[] params = new Object[]{entity.getCourseID(),entity.getClassID(),entity.getCourseName(),entity.getYear(),entity.getTerm(),entity.getHour(),oldId};
        int update = jdbcTemplate.update(sql,params);
        return update;
    }

    public int deleteCourseById(String courseId) {
        String sql = "update sys_course set del_flag = 1 where courseID = ?";
        Object[] params = new Object[]{courseId};
        int delete = jdbcTemplate.update(sql,params);
        return delete;
    }

    public int deleteCourse(String[] ids) {
        int count = 0;
        String sql = "update sys_course set del_flag = 1 where courseID = ?";
        for (int i = 0; i < ids.length; i++){
            Object[] params = new Object[]{ids[i]};
            int delete = jdbcTemplate.update(sql,params);
            count += delete;
        }
        return count;
    }

    public List<String> selectCourseIds() {
        String sql = "select courseID from sys_course ";
        List<String> ids = jdbcTemplate.queryForList(sql,String.class);
        return ids;
    }

    public List<String> selectCourseYear() {
        String sql = "select distinct year from sys_course where del_flag = 0";
        List<String> year = jdbcTemplate.queryForList(sql, String.class);
        return year;
    }

    public List<Course> selectCourseIDAndName(String stuID) {
        String sql = "select courseID,courseName from sys_course,sys_student,sys_classes " +
                "where sys_course.del_flag = 0 and sys_student.del_flag = 0 and sys_classes.del_flag = 0 and " +
                "sys_student.classID = sys_classes.classID and sys_classes.classID = sys_course.classID " +
                "and sys_student.stuID = ? ";
        Object[] params = new Object[]{stuID};
        BeanPropertyRowMapper<Course> classesBeanPropertyRowMapper = new BeanPropertyRowMapper<Course>(Course.class);
        List<Course> courseList = jdbcTemplate.query(sql, classesBeanPropertyRowMapper,params);
        return courseList;
    }

    public List<Course> selectCourseByClassID(String classId) {
        String sql = "select * from sys_course where classID = ? and del_flag = 0";
        BeanPropertyRowMapper<Course> courseBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Course.class);
        Object[] params = new Object[]{classId};
        List<Course> query = jdbcTemplate.query(sql, courseBeanPropertyRowMapper, params);
        return query;
    }
}
